package com.ycw.xiaocai.controller;

import java.io.File;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ycw.bean.BasicInfo;
import com.ycw.bean.ProDetaile;
import com.ycw.bean.ProInfo;
import com.ycw.bean.Projects;
import com.ycw.bean.Users;
import com.ycw.service.xiaocai.FileUploadService;
import com.ycw.service.xiaocai.InsertProjectsService;
import com.ycw.util.Msg;

@Controller
public class FileUploadController {
	
	@Autowired
	FileUploadService fileUploadService;
	@Autowired
	//合并到projects
	InsertProjectsService insertProjectsService;

	// 实现项目图片上传
	@RequestMapping(value="/Upload",method=RequestMethod.POST)
    @ResponseBody
    public Msg upLoad(@RequestParam("file")MultipartFile file,ProDetaile proDetaile,HttpSession session,Projects projects)  {
		//System.out.println("文件上传");
		System.out.println("------------"+proDetaile.toString());
		if (!file.isEmpty()) {//判断文件是否为空
			File files=new File("D:/downloads");
			if(!files.exists()){//如果文件夹不存在
				files.mkdir();//创建文件夹
			}
			String fileName = file.getOriginalFilename();//获取文件名字
			try {
				File file2 = new File(files+File.separator+fileName);
	                file.transferTo(file2);//把文件写入目标文件地址
	                proDetaile.setProdetaileVedio(file2.getAbsolutePath());
	                fileUploadService.UploadPhoto(proDetaile);
	                session.setAttribute("proDetaile", proDetaile); 
	                //得到session对象
	                BasicInfo basicInfo= (BasicInfo)session.getAttribute("basicInfo");
	                ProInfo proInfo=(ProInfo)session.getAttribute("proInfo");
	                ProDetaile proDeta= (ProDetaile)session.getAttribute("proDetaile");
	                //Users users=(Users)session.getAttribute("users");
	                projects.setPsId(basicInfo.getBasicinfoUsId());
	                projects.setPsCustAddress(basicInfo.getBasicinfoCustAddress());
	                projects.setPsCustName(basicInfo.getBasicinfoCustName());
	                projects.setPsCustPhone(basicInfo.getBasicinfoCustPhone());
	                projects.setPsPstId(basicInfo.getBasicinfoPstId());
	                
	                projects.setPsId(proInfo.getProinfoUsId());
	                projects.setPsName(proInfo.getProinfoName());
	                projects.setPsGoal(proInfo.getProinfoGoal());
	                projects.setPsMoney(proInfo.getProinfoMoney());
	                projects.setPsAddress(proInfo.getProinfoAddress());
	                projects.setPsDays(proInfo.getProinfoDatenum());
	                //projects.setPsRepay(proInfo.getProinfoPsRepay());
	                
	                projects.setPsId(proDeta.getProdetaileUsId());
	                projects.setPsVideo(proDeta.getProdetaileVedio());
	                projects.setPsStory(proDeta.getProdetaileStory());
	                projects.setPsSupport(proDeta.getProdetaileSupport());
	                projects.setPsRepay(proDeta.getProdetaileRepay());
	                projects.setPsAboutme(proDeta.getProdetaileAboutme()); 
	                System.out.println("=====合并projects"+projects.toString());
	                insertProjectsService.insertProjects(projects);
	                
	            } catch (Exception e) {

	                e.printStackTrace();

	                return Msg.fail();

	            }

		}
		 return Msg.success();//返回到成功页面
	 }
}
	
 